﻿using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using WFC.BL.DomainModels;

namespace WFC.BL.Repositories
{
    internal static class NHibernateRepository
    {

        private const string DfltConn = "WFCData";

        internal static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MySQLConfiguration.Standard
                    .ConnectionString(c => c
                        .FromConnectionStringWithKey(DfltConn)
                    )
                )
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Content>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Post>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<User>())
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }

        private static void BuildSchema(Configuration config)
        {
            new SchemaExport(config)
                .Create(false, true);
        }

    }
}
